#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/11/27 14:56
# @Author  : Zhang
# @Site    : 数据合法性验证，配合正则表达式
# @File    : form.py
# @Software: PyCharm

import re
from django import forms
from django.core.exceptions import ValidationError

"""
自定义手机号码验证
"""
def mobile_validate(value):
    mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
    if not mobile_re.match(value):
        raise ValidationError('手机号码格式错误')


"""
下拉框的使用方法
0.1实际要存储的值
"""
class PublishForm(forms.Form):
    user_type_choice = (
        (0, u'普通用户'),
        (1, u'高级用户'),
    )

    user_type = forms.IntegerField(widget=forms.widgets.Select(choices=user_type_choice,
                                                               attrs={'class': "form-control"}))

    title = forms.CharField(max_length=20,
                            min_length=5,
                            error_messages={'required': u'标题不能为空',
                                            'min_length': u'标题最少为5个字符',
                                            'max_length': u'标题最多为20个字符'},
                            widget=forms.TextInput(attrs={'class': "form-control",
                                                          'placeholder': u'标题5-20个字符'}))

    memo = forms.CharField(required=False,
                           max_length=256,
                           widget=forms.widgets.Textarea(
                               attrs={'class': "form-control no-radius", 'placeholder': u'详细描述', 'rows': 3}))

    phone = forms.CharField(validators=[mobile_validate, ],
                            error_messages={'required': u'手机不能为空'},
                            widget=forms.TextInput(attrs={'class': "form-control",
                                                          'placeholder': u'手机号码'}))

    email = forms.EmailField(required=False,
                             error_messages={'required': u'邮箱不能为空', 'invalid': u'邮箱格式错误'},
                             widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': u'邮箱'}))


